﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Utils;

namespace NDK.Module.DAL
{
    public class CameraModel
    {
        public int ID { get; set; }
        public string CameraName { get; set; }
        public string CameraSN { get; set; }
        public string CameraPath { get; set; }
        public string CameraIP { get; set; }
        public string CameraType { get; set; }
        public double ExpureTime { get; set; }
    }
    class CameraServices
    {
        /// <summary>
        /// 创建相机表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="conn"></param>
        public void CreatCameraTable(string tableName, SQLiteConnection conn)
        {
            string sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(ID integer PRIMARY KEY AUTOINCREMENT, Name varchar(50) UNIQUE NOT NULL,  CameraIP varchar(20),CameraSN varchar(20), LightCOM varchar(20), LightChanel varchar(20) )";
            SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
            cmdCreateTable.ExecuteNonQuery();
        }

        /// <summary>
        /// 通过相机名称，得到一个对象实体
        /// </summary>
        public CameraModel GetModel(string Name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,CameraName,CameraSN,CameraPath,CameraIP,CameraType,ExpureTime from CameraList ");
            strSql.Append(" where CameraName=@CameraName");
            SQLiteParameter[] parameters = {
            new SQLiteParameter("@CameraName", DbType.String,50)};
            parameters[0].Value = Name;

            CameraModel model = new CameraModel();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.CameraName = ds.Tables[0].Rows[0]["CameraName"].ToString();
                model.CameraSN = ds.Tables[0].Rows[0]["CameraSN"].ToString();
                model.CameraPath = ds.Tables[0].Rows[0]["CameraPath"].ToString();
                model.CameraIP = ds.Tables[0].Rows[0]["CameraIP"].ToString();
                model.ExpureTime = Convert.ToDouble(ds.Tables[0].Rows[0]["ExpureTime"].ToString());
                model.CameraType = ds.Tables[0].Rows[0]["CameraType"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得所有数据
        /// </summary>
        public DataSet GetAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM CameraList ");
            return SQLiteUtils.Query(strSql.ToString());
        }

        #region  自动生成
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Camera");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            return SQLiteUtils.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(CameraModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Camera(");
            strSql.Append("ID,CameraName,CameraSN,CameraPath,Station,CameraIP,IsEnable)");
            strSql.Append(" values (");
            strSql.Append("@CameraName,@CameraSN,@CameraPath,@Station,@CameraIP,@IsEnable)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@CameraName", DbType.String,50),
                    new SQLiteParameter("@CameraSN", DbType.String,20),
                    new SQLiteParameter("@CameraPath", DbType.String,20),
                    new SQLiteParameter("@Station", DbType.String,20),
                    new SQLiteParameter("@CameraIP", DbType.String,20),
                    new SQLiteParameter("@IsEnable", DbType.String,20)};
            parameters[0].Value = model.CameraName;
            parameters[1].Value = model.CameraSN;
            parameters[2].Value = model.CameraPath;
            parameters[3].Value = model.CameraIP;
            parameters[4].Value = model.ExpureTime;
            SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CameraModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Camera set ");
            strSql.Append("CameraName=@CameraName,");
            strSql.Append("CameraSN=@CameraSN,");
            strSql.Append("CameraPath=@CameraPath,");
            strSql.Append("Station=@Station,");
            strSql.Append("CameraIP=@CameraIP,");
            strSql.Append("IsEnable=@IsEnable");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@CameraName", DbType.String,50),
                    new SQLiteParameter("@CameraSN", DbType.String,20),
                    new SQLiteParameter("@CameraPath", DbType.String,20),
                    new SQLiteParameter("@Station", DbType.String,20),
                    new SQLiteParameter("@CameraIP", DbType.String,20),
                    new SQLiteParameter("@IsEnable", DbType.String,20),
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = model.CameraName;
            parameters[1].Value = model.CameraSN;
            parameters[2].Value = model.CameraPath;
            parameters[3].Value = model.CameraType;
            parameters[4].Value = model.CameraIP;
            parameters[5].Value = model.ExpureTime;
            parameters[6].Value = model.ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CameraList ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CameraList ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = SQLiteUtils.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CameraModel GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,CameraName,CameraSN,CameraPath,Station,CameraIP,IsEnable from CameraList ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            CameraModel model = new CameraModel();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.CameraName = ds.Tables[0].Rows[0]["CameraName"].ToString();
                model.CameraSN = ds.Tables[0].Rows[0]["CameraSN"].ToString();
                model.CameraPath = ds.Tables[0].Rows[0]["CameraPath"].ToString();
                model.CameraType = ds.Tables[0].Rows[0]["CameraType"].ToString();
                model.CameraIP = ds.Tables[0].Rows[0]["CameraIP"].ToString();
                model.ExpureTime = Convert.ToDouble(ds.Tables[0].Rows[0]["ExpureTime"].ToString());
                return model;
            }
            else
            {
                return null;
            }
        }
        public List< CameraModel> GetAllModel()
        {
            List<CameraModel> paramsTemp = new List<CameraModel>();
            paramsTemp.Clear();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from CameraList ");
            CameraModel model = null;
            DataSet ds = SQLiteUtils.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    model = new CameraModel();
                    if (ds.Tables[0].Rows[i]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                    }
                    model.CameraName = ds.Tables[0].Rows[i]["CameraName"].ToString();
                    model.CameraSN = ds.Tables[0].Rows[i]["CameraSN"].ToString();
                    model.CameraPath = ds.Tables[0].Rows[i]["CameraPath"].ToString();
                    model.CameraType = ds.Tables[0].Rows[i]["CameraType"].ToString();
                    model.CameraIP = ds.Tables[0].Rows[i]["CameraIP"].ToString();
                    model.ExpureTime = Convert.ToDouble(ds.Tables[0].Rows[i]["ExpureTime"].ToString());
                    paramsTemp.Add(model);
                }
            }
            if (paramsTemp.Count > 0) {
                return paramsTemp;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,CameraName,CameraSN,CameraPath,Station,CameraIP,IsEnable ");
            strSql.Append(" FROM Camera ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteUtils.Query(strSql.ToString());
        }


        #endregion  Method
    }
}
